import { ref } from 'vue';
import { getDeptUserCount, getServiceUserCount, sendDeptUserNote, sendServiceUserNote, sendDeptUserItemNote, sendServiceUserItemNote } from '@src/api/DepartmentApi.js';
import { t } from '@src/locales';
import { debounce } from 'lodash';
import Platform from '@src/util/platform.ts';
import { getRootWindowInitData } from '@src/util/window'
export const useStateNoteUserSendMessage = () => {
  const loading = ref(false);
  const sendLoading = ref(false)
  const totalUser = ref(0);
  const loginUser = ref(0);
  const showDialog = ref(false);
  const haveUserNote = ref(false);

  function initHaveUserNote(){
    // 临时不上
    // haveUserNote.value = false
    haveUserNote.value = getRootWindowInitData()?.tenantType == 1;
  }

  initHaveUserNote()

  const typeDesEnum: any = {
    0: t('department.label.label8'),
    1: t('department.label.label7'),
  };
  const typeClassEnum: any = {
    0: 'not-login-status-box overHideCon-1',
    1: 'have-login-status-box overHideCon-1',
  };
  function findThisStatus(info) {
    if (Reflect.has(info, 'firstLogin') && info['firstLogin'] != 0) {
      return 1;
    }
    return 0;
  }

  function findThisStatusDes(info) {
    const type: any = findThisStatus(info);
    return typeDesEnum[type];
  }

  function findThisStatusClass(info) {
    const type: any = findThisStatus(info);
    return typeClassEnum[type];
  }

  function findThisStatusShowNote(info) {
    const type: any = findThisStatus(info);
    return type === 0;
  }

  function getNowUserCount(type, options) {
    let fnc = null;
    const options_:any = {}
    if (type === 'dept') {
      fnc = getDeptUserCount;
    } else if (type === 'service') {
      fnc = getServiceUserCount;
      options_['tenantProviderId'] = options.tenantProviderId
    }
    loading.value = true;
    fnc && fnc(options_)
      .then(res => {
        if (res.status === 0) {
          totalUser.value = res.data.total || 0;
          loginUser.value = res.data.noLoggedIn || 0;
        }
      })
      .finally(() => {
        setTimeout(() => {
          loading.value = false;
        }, 500);
      });
    
  }

  const noteItem = debounce(function(type, options) {
    let fnc = null;
    const options_:any = {}
    if (type === 'dept') {
      fnc = sendDeptUserItemNote;
      options_['userId'] = options.userId;
    } else if (type === 'service') {
      fnc = sendServiceUserItemNote;
      options_['userId'] = options.loginUserId;
    }
    fnc && fnc(options_).then(res => {
      if (res.status === 0) {
        Platform.toast(t('department.label.label9'));
      } else {
        Platform.toast(res.message, 'error');
      }
    });
  }, 500);

  const noteAll = debounce(function(type, options) {
    let fnc = null;
    const options_:any = {};
    if (type === 'dept') {
      fnc = sendDeptUserNote;
    } else if (type === 'service') {
      fnc = sendServiceUserNote;
      options_['tenantProviderId'] = options.id
    }
    sendLoading.value = true
    fnc && fnc(options_).then(res => {
      if (res.status === 0) {
        showDialog.value = false
        Platform.toast(t('department.label.label9'));
      } else {
        Platform.toast(res.message, 'error');
      }
    }).finally(()=>{
      setTimeout(()=>{
        sendLoading.value = false;
      }, 500)
    });
  }, 500);
  return {
    totalUser,
    loginUser,
    findThisStatus,
    findThisStatusDes,
    findThisStatusClass,
    getNowUserCount,
    loading,
    findThisStatusShowNote,
    noteItem,
    noteAll,
    showDialog,
    haveUserNote,
  };
};
